package com.majie.jedis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.SerializationUtils;
import org.springframework.util.StringUtils;
import redis.clients.jedis.Jedis;
import redis.clients.jedis.JedisPool;

import java.util.Set;
import java.util.TreeSet;

/**
 * jedis工具类
 *
 * @author Administrator
 */
public class JedisService {

    @Autowired
    private JedisPool jedisPool;

    /**
     * 缓存一个对象
     *
     * @param key
     * @param val
     */
    public void setRedisObject(String key, Object val) {
        if (StringUtils.isEmpty(key) || val == null) {
            return;
        }
        Jedis jedis = getJedis();
        try {
            jedis.set(key.getBytes(), SerializationUtils.serialize(val));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(jedis);
        }
    }

    /**
     * 缓存一个对象
     *
     * @param key
     * @param val
     * @param seconds
     */
    public void setRedisObject(String key, Object val, int seconds) {
        if (StringUtils.isEmpty(key) || val == null) {
            return;
        }
        Jedis jedis = getJedis();
        try {
            jedis.setex(key.getBytes(), seconds, SerializationUtils.serialize(val));
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(jedis);
        }
    }

    /**
     * 根据缓存key取出一个对象
     *
     * @param key
     * @return
     */
    public Object getRedisObject(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        Jedis jedis = getJedis();
        try {
            byte[] bs = jedis.get(key.getBytes());
            if (bs != null) {
                Object object = SerializationUtils.deserialize(bs);
                return object;
            }
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(jedis);
        }
        return null;
    }

    /**
     * 删除对象
     *
     * @param key
     * @return
     */
    public boolean delete(String key) {
        if (StringUtils.isEmpty(key)) {
            return false;
        }
        Jedis jedis = getJedis();
        try {
            Long count = jedis.del(key);
            return (count != null && count > 0);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(jedis);
        }
        return false;
    }

    /**
     * 设置string 值
     *
     * @param key
     * @param val
     * @param expiredSeconds
     * @return
     */
    public String set(String key, String val, int expiredSeconds) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        Jedis jedis = getJedis();
        try {
            return jedis.setex(key, expiredSeconds, val);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(jedis);
        }
        return null;
    }

    public void set(String key, String val) {
        if (StringUtils.isEmpty(key)) {
            return;
        }
        Jedis jedis = getJedis();
        try {
            jedis.set(key, val);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(jedis);
        }
    }

    public Double zincrby(String key, double score, String member) {
        Jedis jedis = getJedis();
        try {
            return jedis.zincrby(key, score, member);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(jedis);
        }
        return 0.0;
    }

    public Set<String> zrevrange(String key, Long start, Long fetchSize) {
        Jedis jedis = getJedis();
        try {
            return jedis.zrevrange(key, start, fetchSize);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(jedis);
        }
        return new TreeSet<String>();
    }

    /**
     * 获取Jedis实例
     *
     * @return
     */
    public synchronized Jedis getJedis( ) {
        try {
            if (jedisPool != null) {
                Jedis resource = jedisPool.getResource();
                return resource;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }
        return null;
    }

    /**
     * 释放jedis资源
     *
     * @param jedis
     */
    public void closeResource(Jedis jedis) {
        if (jedis != null) {
            jedis.close();
        }
    }

    /**
     * 获取string对象
     *
     * @param key
     * @return
     */
    public String get(String key) {
        if (StringUtils.isEmpty(key)) {
            return null;
        }
        Jedis jedis = getJedis();
        try {
            return jedis.get(key);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(jedis);
        }
        return null;
    }

    /**
     * 设置超时时间
     *
     * @param key
     * @param expiredSeconds
     */
    public void expire(String key, int expiredSeconds) {
        if (StringUtils.isEmpty(key)) {
            return;
        }
        Jedis jedis = getJedis();
        try {
            jedis.expire(key, expiredSeconds);
        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            closeResource(jedis);
        }

    }

}
